// Load the components and engine objects
Engine.include("/components/component.transform2d.js");
Engine.include("/components/component.mouseinput.js");
Engine.include("/engine/engine.object2d.js");

Engine.initObject("Crosshairs", "Object2D", function() {
    
    var Crosshairs = Object2D.extend({
	
	color: "#000000",		// The color of the object
	center: Point2D.create(0,0),
	width: 0.2,
	height: 0.2,
	areaToChooseFrom: null,
	selected: false,
	selectedPosition: null,
	
	constructor: function(areaToChooseFrom) {
            this.base("Crosshairs");

	    // Add the component to move the object
            this.add(Transform2DComponent.create("move"));

	    // Add the component which handles mouse input
	    this.add(MouseInputComponent.create("input"));

	    // Initialize the position
	    this.setPosition(this.center);

	    this.areaToChooseFrom = areaToChooseFrom;
	},
	
	/**
       * Update the object within the rendering context.  
       * This calls the transform
       * components to position the object on the playfield.
       *
       * @param renderContext {RenderContext} The rendering context
       * @param time {Number} The engine time in milliseconds
       */
	update: function(renderContext, time) {
	    if( this.isSelected() ){
		renderContext.add(GameObject.create(this.selectedPosition));
		renderContext.remove(this);
	    }

	    renderContext.pushTransform();
	    
	    // The the "update" method of the super class
	    this.base(renderContext, time);
	    
	    // Draw the object on the render context
	    this.draw(renderContext);
	    
	    renderContext.popTransform();
	},
	/**
	 * Handle a "mousemove" event from the <tt>MouseInputComponent</tt>.
	 * @param keyCode {Number} The key which was released
	 */
	onMouseMove: function(mouseInfo) {
	    Engine.addMetric("mousepos", mouseInfo.position.x.toFixed(1),
			     false, "#");
	    var pos = Nonlinear.pixel2coordinate(mouseInfo.position);
	    this.setPosition(pos);
	    return false;
	},

	/**
	 * Handle a "keyup" event from the <tt>KeyboardInputComponent</tt>.
	 * @param keyCode {Number} The key which was released
	 */
	onMouseDown: function(mouseInfo) {
	    var pos = this.getPosition();
	    if(this.areaToChooseFrom.containsPoint(pos)){
		this.selectedPosition = pos;
		this.selected = true;
	    }
	    return false;
	},

	/**
        * Returns true is a point has been selected by the user.
        */
	isSelected: function() {
	    return this.selected;
	},

        /**
       * Get the position of the object from the transform component.
       * @return {Point2D}
       */
	getPosition: function() {
            return this.getComponent("move").getPosition();
	},
	
	/**
       * Set the position of the object through transform component
       * @param point {Point2D} The position to draw the text in the playfield
       */
	setPosition: function(point) {
            this.base(point);
            this.getComponent("move").setPosition(point);
	},
	
	/**
	 * Draw our unsafe area onto the specified render context.
	 * @param renderContext {RenderContext} The context to draw onto
	 */
	draw: function(renderContext) {
	    // Draw the unsafe area
	    renderContext.setLineStyle(this.color);
	    renderContext.setLineWidth(0.005);
	    var pt_hor_1 = Point2D.create(this.center.x - this.width/2,
				     this.center.y);
	    var pt_hor_2 = Point2D.create(this.center.x + this.width/2,
				     this.center.y);
	    var pt_vert_1 = Point2D.create(this.center.x, 
				     this.center.y - this.height/2);
	    var pt_vert_2 = Point2D.create(this.center.x,
				     this.center.y + this.height/2);
	    
            renderContext.drawLine(pt_hor_1, pt_hor_2);
	    renderContext.drawLine(pt_vert_1, pt_vert_2);
	}
	
    }, { // Static
	
	/**
       * Get the class name of this object
       * @return {String} The string MyObject
       */
	getClassName: function() {
            return "Crosshairs";
	}
    });
    
    return Crosshairs;
    
});
